// index.js
// 获取应用实例
import pieceApi from '../../api/piece'
import brandApi from '../../api/brand'
import { createStoreBindings } from "mobx-miniprogram-bindings"
import store from "../../store/piece"

const app = getApp()

Page({
  data: {
    freshing:false,
    triggered:true,
    moreLoading:false,
    allFlg:false,
    arr:[],
    show:false,
    param:'',
    brandList:[],
    brandId:[],
    zsl:[],
    jkbz:[],
    jkbzList:[
      {
        name:"进口",
        id:1
      },
      {
        name:"国产",
        id:2
      }
    ],
    mcColor:[],
    mcColorList:[
      {
        name:"灰变",
        id:1
      },
      {
        name:"茶变",
        id:2
      }
      ,
      {
        name:"绿变",
        id:3
      }
    ],
    zslList:[
      {
        name:"1.50",
        id:1.50
      },
      {
        name:"1.55",
        id: 1.55
      },
      {
        name:"1.60",
        id: 1.60
      },
      {
        name:"1.67",
        id:1.67
      },
      {
        name:"1.71",
        id:1.71
      },
      {
        name:"1.74",
        id:1.74
      }
    ],
    zslIndex:null
  },
  onLoad(data) {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields:['formData'],
      actions: { updateProdInFo: "updateProdInFo" }
    })
    this.storeBindings.updateStoreBindings()
    this.prodSearch()

    this.getBrandList()
   
  },
  getBrandList() {
    brandApi.brandList().then((res)=>{
      this.setData({
        brandList:res
      })
  })
  },
  prodSearch() {
    let data = this.data.formData.formData
    data.zsl = this.data.zsl
    data.mcColor = this.data.mcColor
    data.jkbz = this.data.jkbz
    data.brandId = this.data.brandId
    // JSON.stringify(data)
    console.log("onRefresh---prodSearch-----",data)
    pieceApi.prodSearch(JSON.stringify(data)).then((res)=>{
        this.setData({
          arr:res
        })
    })
  },
  onUnload() {
    // 解绑
    this.storeBindings.destroyStoreBindings()
  },
  onRefresh() {
    if (this.freshing) return
    this.freshing = true
    console.log("onRefresh---下拉-----")
    setTimeout(() => {
      this.setData({
        triggered: false,
      })
      this.freshing = false
    }, 3000)
  
  },
  scrollBottom() {
    console.log("scrollBottom---上拉-----")
  },
  onClose() {

  },
  onClick() {
    this.setData({
      show:true
    })
    // let data1 = this.data.formData.formData
    // data1.param = this.data.param
    // this.prodSearch(data1)
  },
  onBottomClick(e) {
    let  a = e.currentTarget.dataset.item
    this.updateProdInFo(a)
    wx.navigateTo({
      url: '../prodInfo/prodInfo?id='+a.id
    })
  },
  onResetClick() {
    this.setData({
      show:false,
      zsl:[],
      mcColor:[],
      brandId:[],
      jkbz:[]
    })
    this.prodSearch()
  },
  onConfirmClick(){
      this.setData({
        show:false
      })
      this.prodSearch()
  },
  onBrandChange(event) {
    this.setData({
      brandId: event.detail,
    });
  },
  onMcColorChange(event) {
    this.setData({
      mcColor: event.detail,
    });
  },
  zslPickerChange(event) {
    this.setData({
      zslIndex: event.detail.value,
    });
   
  },
  onJkbzChange(event) {
    this.setData({
      jkbz: event.detail,
    });
  },
  onZslChange(event) {
    this.setData({
      zsl: event.detail,
    });
  },
  searchBtn(event) {
    this.prodSearch()
  }


})
